<?php

class Member extends AppModel {
	
	var $name = 'Member';
	var $primaryKey = 'member_id';
	var $hasOne = array('Producer'=>
	array('className'=>'Producer', 'dependent'=>true)
	);
	var $hasMany = array('BasketItem');
	
	var $validate = array(
		'username' => '/^[a-z0-9\_\-]{3,}$/i',
		'password' => VALID_NOT_EMPTY,
		'first_name' => VALID_NOT_EMPTY,
		'last_name' => VALID_NOT_EMPTY,
		'email' => VALID_EMAIL
	);
	
	function _c_find_sql($find_options = array()) {
		
		$auth_type = null;
		$order_open = null;
		$order = 'Member.last_name, Member.first_name';
		$votes = null;
		$volunteer = null;
		$board = null;
		$discontinued = null;
		$includeDiscontinued = null;
		$paid = null;
		$unpaid = null;
		$due = null;
		extract($find_options);
		
		
		$search = array();
		$needs = array();
		
		if (($order_open === null) || ($order_open == 0)) {
		} else {
			$search[] = 'Member.member_id = Basket.member_id';
			$search[] = 'Basket.orderable_id = Orderable.id';
			$search[] = 'Orderable.product_id = Product.product_id';
			$search[] = 'Orderable.delivery_id = ' . $order_open;
			$needs['basket_items'] = 'Basket';
			$needs['orderables'] = 'Orderable';
			$needs['product_list'] = 'Product';
		}
		
		if (($auth_type === null) || (!$auth_type)) {
		} else if (($auth_type == AUTH_PROD) && ($order_open !== null)){
			$search[] = 'Product.producer_id = Producer.producer_id';
			$search[] = 'Member.member_id = Producer.member_id';
			$needs['product_list'] = 'Product';
			$needs['producers'] ='Producer';
			if ($search[0] == 'Member.member_id = Basket.member_id') { unset($search[0]); }
		} else {
			$search[] = 'Member.auth_type = "' . $auth_type . '"';
		}
		
		
		$search[] = 'Member.no_email = 0'; // must be accepting email
		$search[] = 'Member.email_invalid = 0'; // email address must not be marked invalid, either
		if ($votes) {
			$search[] = 'Member.votes = "1"';
		}
		if ($volunteer) {
			$search[] = 'Member.volunteer = "1"';
		}
		if ($board) {
			$search[] = 'Member.board_mem = "1"';
		}
		if ($discontinued) {
			$search[] = 'Member.membership_discontinued = "1"';
		}
		if($paid) {
			$search[] = "Member.paid_fee > '" . date('Y-m-d', time() - 31536000) . "'";
		}
		if ($unpaid) {
			$search[] = "Member.paid_fee < '" . date('Y-m-d', time() - 31536000) . "'";
		}
		if ($due) {
			$search[] = "Member.paid_fee > '" . date('Y-m-d', time() - 31536000) . "' AND Member.paid_fee < '" . date('Y-m-d', time() - 28944000) . "'";
		}
		
		if (!$discontinued && !$includeDiscontinued) {
			$search[] = 'Member.membership_discontinued = "0"';
		}
		
		$tables = array('members as Member');
		foreach($needs as $table_name => $as) {
			$tables[] = $table_name . ' as ' . $as;
		}
		$table = join(', ', $tables);
		$search = join(' AND ', $search);
		
		$join = '';
		if (!isset($needs['producers'])) {
			$join = 'LEFT JOIN producers as Producer ON (Member.member_id = Producer.member_id)';
		}
		
		if (empty($search)) { $search = '1'; }
		
		return 'SELECT DISTINCT Member.member_id, Member.business_name, Member.first_name, Member.last_name, Member.first_name_2, Member.last_name_2, Member.email_address, Member.email_address_2, Member.address_line1, Member.address_line2, Member.city, Member.state, Member.zip, Member.work_address_line1, Member.work_address_line2, Member.work_city, Member.work_state, Member.work_zip, Member.home_phone, Member.work_phone, Member.mobile_phone, Member.fax, Member.membership_date, Member.votes, Member.paid_fee, Producer.producer_id
		FROM (' . $table . ') ' . $join . ' WHERE ' . $search . ' ORDER BY ' . $order;
	
	}
	
	function getEmails($find_options = array(), $page = 1, &$more) {
		//echo ($more);
		//echo($search);
		
		$pageLimit = (isset($find_options['pageLimit'])) ? $find_options['pageLimit'] : 25;
		$limit = 'LIMIT ' . ($pageLimit * ($page-1)) . ',' . $pageLimit;
		
		
		$sql = $this->_c_find_sql($find_options);
		$count = $this->count_c_find($find_options);
		$more = ($count > ($pageLimit * $page)) ? $page + 1 : false;
		return $this->query($sql . ' ' . $limit);
	}
	
	function c_find($find_options = array(), $order = null, $limit = null, $page = null) {
		if (($limit !== null) && ($page !== null)) {
			$limit = 'LIMIT ' . ($limit * ($page-1)) . ',' . $limit;
		} else {
			$limit = '';
		}
		
		if ($order !== null) {
			$find_options['order'] = $order;
		}
		
		return $this->query($this->_c_find_sql($find_options) . ' ' . $limit);
	}
	
	function count_c_find($find_options = array()) {
		$c = $this->query('SELECT COUNT(*) as count FROM (' . $this->_c_find_sql($find_options) . ') as Count');
		
		return $c[0][0]['count'];
		//print_r($c);
	
	}
	
	function getAddressesByProducer($producer_id, $del = null) {
		$d = ($del === null) ? Configure::read('Delivery.delivery_id') : $del;
		$sql = 'SELECT DISTINCT Member.first_name, Member.last_name, Member.address_line1, Member.address_line2, Member.city, Member.state, Member.zip, Member.work_address_line1, Member.work_address_line2, Member.work_city, Member.work_state, Member.work_zip FROM producers as Producer, members as Member, basket_items as Basket, product_list as Product, orderables as Orderable
		WHERE Product.producer_id = "' . $producer_id . '"
		AND Orderable.product_id = Product.product_id
		AND Orderable.id = Basket.orderable_id
		AND Basket.member_id = Member.member_id
		AND Orderable.delivery_id = ' . $d
		. ' ORDER BY Member.last_name, Member.first_name';
		
		$r = $this->query($sql);
		return $r;
	}
	
	function formatAddresses($members) {
		$r = array();
		foreach($members as $member) {
			$a = array();
			$a[] = '<b>' . $member['Member']['first_name'] . ' ' . $member['Member']['last_name'] . '</b>';
			$a[] = $member['Member']['address_line1'];
			if (!empty($member['Member']['address_line2'])) {
				$a[] = $member['Member']['address_line2'];
			}
			$a[] = $member['Member'][$w . 'city'] . ', ' . $member['Member'][$w . 'state'] . ' ' . $member['Member'][$w . 'zip'];
			$r[] = $a;
		}
		return $r;
	}
	
	function isPaid($id) {
		$m = $this->find('Member.member_id = ' . $id);
		return (strtotime($m['Member']['paid_fee'] . ' + 1 year') > time());
	}
	
	function hashPasswords($data) {
		$data[$this->alias]['password'] = "-!PASSWORD('" . $data[$this->alias]['password'] . "')";
		return $data;
	}
	
}

?>